<?php

/*
 * tiers/edit.phtml
 */
?>
<?php
    $title = "Fiches tiers";
    $this->headTitle($title);
    $uplRepPhoto = str_replace('public/', '', $this->rep_upload['photo'])  ;
    $uplRepRib = str_replace('public/', '', $this->rep_upload['rib'])  ;
    $dWk = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche') ;
    
?>



<div class='content-head'>
    <h3>
        <?php $infoTiers = "<span id='title-tiers'>". $tiers->libelle . "</span>" ;?>	        
        <?php echo $title; ?> : <?php echo $infoTiers ;?>  
    </h3>
    <div class="toolbar">   
            <button class='btn btn-inverse' onclick="location.href = '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'index')) ?>';">Revenir à la liste des tiers</button>            
    </div>
    
</div>

<div class='content-body'>
    <div class="content-body tiers-border" style='min-height: 500px;'>
        <div class="tiers-top ">        
             <div class="inf-tiers inf-tiers-grise" style="margin-top: 20px;"> 
             Libellé : <a id= 'tiers-libelle' href='#' class=''  data-name="libelle" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->libelle; ?></a>
             </div>
         </div>
        <div class='tiers-left '>
             <div class="cnt-photo-tiers" >
                    <a href='javascript: void(0);' class='cnt-img-tiers' id='link-uploadPhoto'>
                        <?php 
                        $photo = $tiers->photo && $tiers->photo!=='' ? $tiers->photo : null; 
                        
                        $basePath	 =  $this->basePath(); 
                        $photoUrl = $basePath . ($photo? '/'. $uplRepPhoto . $photo : '/img/rhx/tiers-default.jpg'); 
                        ?>

                    <img id="tiers-photo" src="<?php echo  $photoUrl ;?>" width="320" height="auto">
                    </a>
                    <a href="#" id="btn-uploadPhotoTiers" data-id="<?php echo $tiers->id; ?>"  class='btn btn-info btn-small' style="display:none; z-index:55555;">
                            Télécharger photo
                    </a>
             </div>  
            
            <div class="inf-tiers inf-tiers-pink">
                Contact : <a id= 'tiers-contact' href='#' class=''  data-name="contact" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->contact; ?></a></div>     
                
                <div class="inf-tiers inf-tiers-pink">
                Siret : <a id= 'tiers-siret' href='#' class=''  data-name="siret" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->siret; ?></a></div>     
                
                <div class="inf-tiers inf-tiers-pink">
                Site web : <a id= 'tiers-website' href='#' class=''  data-name="website" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->website; ?></a></div>     
                
        </div>
       
        <div class='tiers-right'>
            <div class="inf-tiers inf-tiers-blue">
                Adresse : <a id= 'tiers-adresse' href='#' class=''  data-name="adresse" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->adresse; ?></a>
            </div>     
                
            <div class="inf-tiers inf-tiers-blue">
                Téléphone : <a id= 'tiers-phone' href='#' class=''  data-name="phone" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->phone; ?></a>
            </div>
            <div class="inf-tiers inf-tiers-blue">
                Fax : <a id= 'tiers-fax' href='#' class=''  data-name="fax" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->fax; ?></a>
            </div>
            <div class="inf-tiers inf-tiers-blue">
                Mail : <a id= 'tiers-mail' href='#' class=''  data-name="mail" data-pk=" <?php echo $tiers->id; ?>" data-type="text" ><?php echo $tiers->mail; ?></a>
            </div>
	    
	     <div class='cnt-inf-tiers cnt-inf-tiers-rib_top clear-auto' style='margin-top: 20px;'>            
		    <div class='inf-tiers inf-tiers-yellow ' > RIB :   
			<?php $hasRib  = $tiers->rib && $tiers->rib != '' && $tiers->rib != '#' ;?>		
			<a href='<?php echo $this->basePath() .'/'. $uplRepRib .$tiers->rib ?>' class='btn btn-success btn-small' target='_blank' id='link-viewRib' style='display: <?php echo $hasRib? '':'none' ;?>;'>Voir</a>
			<a href='javascript: void(0);' class='bt btn btn-info btn-small' id= 'link-uploadRib' >Télécharger</a>			
		    </div>     


	       </div>
            
            <div class='cnt-inf-tiers clear-auto' style='margin-top: 20px;'>                
           
                <div class='inf-tiers inf-tiers-white inf-tiers-border-blue' style='margin-bottom: 6px;'> 
                         Horaire du Tiers 
                </div>
                <table id='table-tiers-horaire' class='table table-striped '>
                 <thead>
                  <tr>
                      <?php foreach ($dWk as $d ): ?>
                      <td width='9%'>
                          <?php echo strtoupper($d[0]);?> 

                      </td>
                      <?php endforeach ;?>


                  </tr>
                 </thead>

                  <tbody>
                       <tr>
                         <?php foreach ($dWk as $d ): ?>
                         <td style=' padding-top: 0 ;padding-bottom: 0 ;'>

                             <input class="checkbox checkbox-open-state" type="checkbox" value='' <?php echo $tiers->$d ? 'checked' : '' ;?> data-pk='<?php echo $tiers->id ;?>' data-name='<?php echo $d; ?>'>
                         </td>
                         <?php endforeach ;?>


                     </tr>
                      <tr> 
                          <?php foreach ($dWk as $d ): ?>
                             <?php $d_ouv = $d. '_ouverture' ;?>

                             <td><a class="hr-ouverture hr-ouverture-<?php echo $d;?>" data-title="Heure d'ouverture le <?php echo $d;?>" data-pk="<?php echo $tiers->id?>" data-name='<?php echo $d_ouv; ?>' data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#" > 
                                     <?php if ($tiers->$d):                                 
                                             $tm = strtotime($tiers->$d_ouv);
                                             $s = sprintf ("%sh %sm", date("H", $tm), date("i", $tm) );

                                           else : 
                                             $s = '&nbsp;' ;
                                           endif ;
                                           echo $s;                                           
                                      ?> 
                                 </a>
                             </td>

                          <?php endforeach ;?>
                      </tr>
                      <tr> 
                           <?php foreach ($dWk as $d ): ?>
                             <?php $d_fer = $d. '_fermeture' ;?>
                             <td><a class="hr-fermeture hr-fermeture-<?php echo $d;?>" data-title="Heure de fermeture le <?php echo $d;?>" data-pk="<?php echo $tiers->id?>" data-name='<?php echo $d_fer; ?>' data-viewformat="HH\h mm\m" data-format="HH:mm" data-template="HH:mm" data-type="combodate" href="#"> 
                                     <?php if ($tiers->$d):                                 
                                             $tm = strtotime($tiers->$d_fer);
                                             $s = sprintf ("%sh %sm", date("H", $tm), date("i", $tm) );

                                           else : 
                                             $s = '&nbsp;' ;
                                           endif ;
                                           echo $s;
                                      ?>
                                 </a>
                             </td>
                          <?php endforeach ;?>
                      </tr>

                  </tbody>      
                 </table>


             <div  class='cnt-inf-tiers cnt-inf-tiers-btns clear-auto'  >           
                <a href='javascript: void(0);' class='bt bt-show ' id='link-obligatoire'>Affichage obligatoire</a>                
            </div>

        </div>
        </div>
            
    </div>
        <div class="clear-auto"> </div>
    </div>
   

</div>

<!-- Script -->
<script type='text/javascript'>
    
    
    var JsTiersEdit = (function(){
        var isEmpty = function(selector){
                return $.trim($(selector).val()) === '';
            },
            isFloat = function(val){
                v = $.trim(val);
                patFloat = new RegExp(/^[0-9]*[.]{0,1}[0-9]+$/);

                return patFloat.test(v);                     
            }
            ;
            
        return {
            init : function(){
                JsTiersEdit.refresh();                
                JsTiersEdit.initErgo();                
                JsTiersEdit.initEditing();                
                JsTiersEdit.initHoraire();                
                JsTiersEdit.initUpload();                
            },
            refresh : function(){
                console.log('refresh');
                
            },
            initEditing : function(){console.log('init initEditing');
                $('#tiers-libelle' ).editable({
                    title: 'Edition du libellé',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>',
                    validate : function(val){
                        if($.trim(val) === '')  
                            return 'Le libellé ne peut être vide';
                    }
                });
                $('#tiers-contact' ).editable({
                    title: 'Edition du contact',
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-adresse' ).editable({
                    title: "Edition de l'adresse",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-siret' ).editable({
                    title: "Edition du siret",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-website' ).editable({
                    title: "Edition du site web",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-phone' ).editable({
                    title: "Edition du Numéro de téléphone",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-fax' ).editable({
                    title: "Edition du fax",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
                $('#tiers-mail' ).editable({
                    title: "Edition de l'adresse e-mail",
                    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>'
                });
            }, initErgo :function (){
                $('.cnt-photo-tiers').hover(function(e){                    
                     $('#btn-uploadPhotoTiers').css('display', '');
                }).mouseleave(function(e){                    
                     $('#btn-uploadPhotoTiers').css('display', 'none');
                });
            } , initUpload : function (){
                // uploading photo
                    $(['#link-uploadPhoto', '#btn-uploadPhotoTiers']).each(function (k, id){
                        $(id).on ('click', function(e){
                           e.preventDefault();
                           uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'uploadPhoto', 'id'		 => $tiers->id)) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
                        });
                    });
                    
                // upload rib                
                $('#link-uploadRib').on('click', function(e){
                        e.preventDefault();
                        
                        uploadWindow = window.open('<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'uploadRib', 'id'		 => $tiers->id)) ?>', 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');
                     });
                     
                $('#link-obligatoire').on('click', function(e){
                        e.preventDefault();
                        alert('En cours de développement');
                        
                     });
                    
                    
            }, initHoraire : function(){
                // horaire tiers
                    $('.hr-ouverture').each(function(){
                        $(this).editable({
                             /*placement: 'right',*/
                             combodate: {
                                 firstItem: 'name',
                                 minuteStep: 5
                             },                
                             url: "<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>",
                             validate: function(val){
                                 new_hour = val.hour()+ 'h '+ val.minute()+ 'm';

                             },
                             success: function(val){
                                 console.log('success');
                             }
                         });           
                    });
                     $('.hr-fermeture').each(function(){
                        $(this).editable({
                             /*placement: 'right',*/
                             combodate: {
                                 firstItem: 'name',
                                 minuteStep: 5
                             },
                             url: "<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>",
                             validate: function(val){
                                 new_hour = val.hour()+ 'h '+ val.minute()+ 'm';
                                 //$(this).html(new_hour);
                             },
                             success: function(val){
                                 console.log('success');
                             }
                         });           
                    });
                    
                    $('.checkbox-open-state').each(function(){
                            var $this = $(this), 
                                init = function(){
                                    var  day = $this.data('name'),
                                         $hrOuv = $('.hr-ouverture-'+ day ),
                                         $hrFer = $('.hr-fermeture-'+ day );

                                    isChecked = $this.is(':checked') ? 1 : 0;

                                    $hrOuv.editable(isChecked ? 'enable' :'disable');
                                    $hrFer.editable(isChecked ? 'enable' :'disable');
                                    if(!isChecked){ 
                                        $hrOuv.html('...'); 
                                        $hrFer.html('...');
                                    }
                                    // design bootstrap
                                    $this.checkbox();                                    
                                };

                            // init checkbox 
                            init();

                            //init onchange
                            $this.on( 'change', function(){
                                var $this = $(this),
                                     updateOpenClose = function (pk, day_openState, val){
                                         _data = '&pk='+pk +'&name=' +day_openState + '&value='+val
                                         ajaxOC = $.ajax({
                                             type: 'post',
                                             url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>',
                                             data: _data
                                         });
                                     }
                                     ;


                                pk = $this.data('pk');
                                day = $this.data('name');
                                isChecked = $this.is(':checked') ? 1 : 0;

                                _data = '&pk='+pk+'&name='+day + '&value='+isChecked ;
                                ajax = $.ajax({
                                     type: 'post',
                                     url: '<?php echo $this->url('RHX/default', array('controller'	 => 'tiers', 'action'	 => 'editData')) ?>',
                                     data: _data
                                 });
                                 ajax.done(function(result) {
                                     var $hrOuv = $('.hr-ouverture-'+ day ),
                                         $hrFer = $('.hr-fermeture-'+ day );

                                     // enable/disable
                                     $hrOuv.editable(isChecked ? 'enable' :'disable');
                                     $hrFer.editable(isChecked ? 'enable' :'disable');

                                     // update 
                                     if(isChecked){ 
                                         updateOpenClose (pk, day+ '_ouverture', '07:00' );
                                         $hrOuv.text('07h 00m').removeClass('editable-empty');

                                         updateOpenClose (pk, day + '_fermeture', '23:00' );
                                         $hrFer.text('23h 00m').removeClass('editable-empty');
                                     }
                                     else{ console.log('.hr-ouverture-' + day);
                                         updateOpenClose (pk, day+ '_ouverture', '00:00' );
                                         $hrOuv.text('...');

                                         updateOpenClose (pk, day + '_fermeture', '00:00' );
                                         $hrFer.text('...');
                                     }

                                 });
                             });           
                        });                          
            }
        } 
    } )();
    
    $(document).bind('ready', JsTiersEdit.init);
</script>
